Codes, Data and Results (markdown):

Please feel free to contact Wanze Xie (WX: wanze.xie@childrens.harvard.edu) or Joe Bathelt (JB: JoeBathelt@gmail.com) for any questions.

"Esc" will take me into command mode where I can navigate around my notebook with arrow keys. Some useful shortcuts can be found here: https://www.dataquest.io/blog/jupyter-notebook-tips-tricks-shortcuts/

In [1]:
import warnings
warnings.filterwarnings("ignore")
In [2]:
import bct
import impyute
import itertools 
import matplotlib.pylab as plt
import matplotlib.patches as patches
from matplotlib.colors import LinearSegmentedColormap
import matplotlib.patches as mpatches
import networkx as nx
import os
import pandas as pd
import numpy as np
import re
import seaborn as sns
from scipy.stats import chisquare, sem, ttest_ind, zscore
from scipy.spatial.distance import pdist, squareform
import statsmodels.api as sm
import statsmodels.formula.api as smf
from statsmodels.stats.multitest import multipletests

The following codes are used by JB to set up the parameters and styles for plots and display (I guess).

In [3]:
sns.set_style('white')

from matplotlib import rcParams  
rcParams['font.family'] = 'serif'  
rcParams['font.serif'] = ['Computer Modern Unicode'] + plt.rcParams['font.serif'] 
rcParams['text.usetex'] = False  
rcParams['axes.labelsize'] = 9  
rcParams['xtick.labelsize'] = 9  
rcParams['ytick.labelsize'] = 9 
rcParams['legend.fontsize'] = 9
mm2inches = 0.039371
single_column = 90*mm2inches
double_column = 190*mm2inches
one_half_column = 140*mm2inches

Function defintions to be used further down. This includes the consus community clustering algorithm

In [4]:
def get_consensus_module_assignment(correlation_matrix):
    # Create the agreement matrix with 50 initial repetitions
    agreement_matrix = []
    gamma = 1.0
    repetitions = 50

    for i in np.arange(0, repetitions):
        # Thresholding weak connections
        correlation_matrix = bct.threshold_absolute(correlation_matrix, 0.1)

        # Community detection
        modules, q = bct.modularity_louvain_und_sign(correlation_matrix, gamma=gamma)

        # Fine-tuning the community solution
        modules, _ = bct.modularity_finetune_und(correlation_matrix, modules, gamma=gamma)

        # Creating the agreement matrix
        module_matrix = np.repeat(modules, repeats=correlation_matrix.shape[0])
        module_matrix = np.reshape(module_matrix, newshape=correlation_matrix.shape)
        agreement_matrix.append(1.*(module_matrix == module_matrix.transpose()))

    agreement_matrix = np.rollaxis(np.asarray(agreement_matrix), 0, 3)
    agreement_matrix = np.sum(agreement_matrix, 2)/repetitions

    # Create the consensus partition (Lancichinetti & Fortunato)
    consensus_partition = bct.consensus_und(agreement_matrix, tau=0.1)
    
    return(consensus_partition, q)

def plot_adjmat(sorted_corrmat, cluster_vector):
    # Set up the matplotlib figure
    f, ax = plt.subplots(figsize=(90 * mm2inches, 80 * mm2inches), dpi=72)

    # Draw the heatmap with the mask and correct aspect ratio
    sns.set_style('white')
    sns.heatmap(sorted_corrmat, vmax=1.0, cbar=False, center=0, cmap='bwr',
                square=True, cbar_kws={"shrink": .5})
    ax.set_xticklabels(' ')
    ax.set_yticklabels(' ')

    # Create a Rectangle patch
    for cluster in np.unique(cluster_vector):
        x = 0 + np.where(sorted(cluster_vector) == cluster)[0][0]
        y = len(cluster_vector) - np.where(sorted(cluster_vector) == cluster)[0][-1]
        width = height = np.where(np.asarray(sorted(cluster_vector)) == cluster)[0][-1] - np.where(np.asarray(sorted(cluster_vector)) == cluster)[0][0]
        rect = patches.Rectangle((x, y),
                                 width,
                                 height,
                                 linewidth=0.5, edgecolor='k', facecolor='none')
        # Add the patch to the Axes
        ax.add_patch(rect)
    plt.savefig('output.jpg')
    plt.show()

loading the data

In [5]:
df = pd.read_csv('../data/raw/tempandquestions_all.csv')
IBQ_columns = [column for column in df.columns if re.search('IBQ', column)]
columns_to_use = np.hstack([['subj', 'Sex'], IBQ_columns])
print(columns_to_use.shape)
print(columns_to_use)
(19,)
['subj' 'Sex' 'IBQ_Activity' 'IBQ_Distress' 'IBQ_Fear' 'IBQ_Orienting'
 'IBQ_Smiling' 'IBQ_High_Pleas' 'IBQ_Low_Pleas' 'IBQ_Soothability'
 'IBQ_Fall_React' 'IBQ_Cuddliness' 'IBQ_Percept_Sens' 'IBQ_Sadness'
 'IBQ_Approach' 'IBQ_Vocal_React' 'IBQ_SURGENCY' 'IBQ_NEGATIVE_EMO'
 'IBQ_EMOTION_REGUL']

Delete the columns of IBQ_DOC (date) and IBQ_Complete (logical variable) WX manually removed those two columns from the data, so don't need to run the following.

columns_to_use = np.delete(columns_to_use,[2,3],0) print(columns_to_use.shape) print(columns_to_use)

In [6]:
df = df[columns_to_use].copy()
print(df.shape)
print(df)
(780, 19)
         subj  Sex  IBQ_Activity  IBQ_Distress  IBQ_Fear  IBQ_Orienting  \
0     s001a5f    1      4.058824      3.058824  1.615385       6.181818   
1     s002a5a    0      4.235294      3.789474  1.562500       2.900000   
2     s003a5f    0      5.058824      2.900000  1.875000       4.111111   
3     s004a5a    1      5.058824      3.105263  3.000000       2.636364   
4     s006a5a    0      3.529412      3.736842  1.300000       3.363636   
5     s007a5f    1      4.058824      3.650000  1.642857       4.000000   
6     s008a7a    1      5.294118      5.200000  3.812500       4.000000   
7     s009a7f    1      4.235294      4.500000  2.750000       3.818182   
8     s010a7a    0      4.294118      3.200000  1.733333       3.181818   
9     s011a7f    1      4.705882      3.437500  2.125000       3.666667   
10    s012a7a    0      5.125000      4.000000  2.000000       2.444444   
11    s013a7f    0      4.812500      3.700000  2.666667       2.818182   
12    s014a7a    0      5.647059      2.800000  1.500000       4.090909   
13    s015a7f    1      4.235294      3.950000  3.400000       4.636364   
14    s016a7a    1      3.235294      3.736842  3.000000       2.545455   
15    s017a7f    1      4.363636      4.062500  2.500000       4.000000   
16    s018a7a    0      4.117647      3.368421  1.466667       4.000000   
17    s019a7f    1      3.764706      3.526316  2.461538       2.181818   
18    s020a5a    1      3.882353      3.850000  2.700000       4.333333   
19    s021a7f    0      5.705882      2.600000  2.000000       2.777778   
20    s022a7a    1      5.312500      4.875000  3.375000       4.272727   
21    s023a7a    1      3.437500      3.500000  3.466667       4.454545   
22    s024a7f    0      4.000000      3.450000  2.187500       4.875000   
23    s025a7a    0      3.705882      2.800000  1.333333       3.181818   
24    s026a7a    1      4.117647      3.950000  1.714286       3.700000   
25    s027a7f    0      4.714286      3.350000  2.714286       3.111111   
26    s028a7a    1      4.058824      2.750000  1.555556       3.636364   
27    s029a7f    0      4.411765      3.550000  2.312500       3.636364   
28    s030a7a    1      2.764706      2.800000  1.750000       2.909091   
29    s031a7f    0      3.764706      3.947368  2.846154       2.727273   
..        ...  ...           ...           ...       ...            ...   
750  s855a12f    1      4.571429      5.428571  4.666667       3.500000   
751   s856a7f    0      5.000000      3.857143  3.666667       2.166667   
752  s857a12f    0      4.000000      3.714286  3.333333       3.000000   
753   s858a7f    1      2.714286      3.857143  1.500000       3.333333   
754   s859a7f    1      5.000000      4.142857  3.500000       1.833333   
755  s860a12f    1           NaN           NaN       NaN            NaN   
756  s861a12f    0      4.285714      5.142857  2.800000       3.500000   
757  s862a12f    1      4.857143      3.714286  1.200000       2.333333   
758  s863a12f    1      4.142857      4.714286  2.400000       3.000000   
759  s864a12f    1      5.285714      5.000000  5.000000       3.000000   
760  s865a12f    0      5.142857      4.285714  4.000000       4.800000   
761  s866a12f    0      4.857143      4.285714  2.000000       2.500000   
762   s867a7f    0      3.142857      4.285714  4.500000       4.000000   
763  s868a12f    1      6.142857      5.857143  3.800000       3.000000   
764  s869a12f    0      4.142857      4.857143  1.600000       2.833333   
765  s870a12f    0      4.000000      2.571429  1.833333       2.500000   
766  s871a12f    1      5.857143      4.857143  3.500000       2.166667   
767  s872a12f    0      4.714286      4.285714  4.000000       2.000000   
768  s873a12f    0      3.571429      2.428571  2.000000       4.500000   
769   s874a7f    0           NaN           NaN       NaN            NaN   
770   s875a7f    0      4.000000      3.857143  2.166667       3.833333   
771   s876a7f    1      4.857143      2.428571  2.833333       4.333333   
772   s877a7f    1      3.000000      4.714286  2.400000       2.833333   
773   s878a7f    1      5.142857      3.428571  1.400000       2.833333   
774  s879a12f    0      4.714286      5.285714  3.833333       3.833333   
775  s880a12f    0      3.285714      2.285714  1.600000       3.333333   
776  s881a12f    0      3.000000      3.428571  1.000000       6.000000   
777  s882a12f    0      5.857143      5.142857  5.166667       4.500000   
778  s883a12f    1      2.714286      4.714286  3.333333       2.833333   
779  s884a12f    1      4.714286      2.714286  3.666667       3.166667   

     IBQ_Smiling  IBQ_High_Pleas  IBQ_Low_Pleas  IBQ_Soothability  \
0       5.200000             NaN            NaN          4.272727   
1       3.666667             NaN            NaN          4.111111   
2       4.153846             NaN            NaN          3.909091   
3       4.266667             NaN            NaN          4.545455   
4       4.800000             NaN            NaN          4.700000   
5       3.666667             NaN            NaN          6.777778   
6       4.533333             NaN            NaN          4.100000   
7       5.333333             NaN            NaN          6.727273   
8       5.400000             NaN            NaN          5.545455   
9       5.266667             NaN            NaN          3.800000   
10      4.857143             NaN            NaN          4.555556   
11      4.142857             NaN            NaN          5.000000   
12      5.428571             NaN            NaN          3.800000   
13      5.533333             NaN            NaN          5.000000   
14      3.066667             NaN            NaN          2.454545   
15      6.769231             NaN            NaN          3.777778   
16      5.933333             NaN            NaN          5.090909   
17      4.714286             NaN            NaN          4.600000   
18      4.615385             NaN            NaN          4.545455   
19      4.133333             NaN            NaN          4.181818   
20      5.666667             NaN            NaN          4.090909   
21      5.266667             NaN            NaN          4.777778   
22      5.133333             NaN            NaN          4.818182   
23      5.714286             NaN            NaN          4.272727   
24      3.466667             NaN            NaN          4.500000   
25      4.428571             NaN            NaN          4.181818   
26      5.923077             NaN            NaN          3.909091   
27      3.800000             NaN            NaN          4.100000   
28      3.384615             NaN            NaN          4.222222   
29      4.214286             NaN            NaN          3.818182   
..           ...             ...            ...               ...   
750     3.571429        6.428571       5.285714          5.857143   
751     3.714286        6.000000       3.857143          5.714286   
752     5.714286        6.285714       6.000000          5.857143   
753     3.857143        5.285714       4.571429          5.571429   
754     4.142857        5.714286       5.000000          5.666667   
755          NaN             NaN            NaN               NaN   
756     5.428571        6.000000       6.000000          6.285714   
757     1.857143        5.000000       4.000000          6.428571   
758     4.000000        7.000000       5.800000          6.571429   
759     5.285714        5.000000       5.000000          5.285714   
760     5.000000        6.833333       5.857143          6.714286   
761     4.142857        4.142857       4.600000          5.800000   
762     5.571429        6.571429       6.142857          4.142857   
763     4.000000        5.714286       6.000000          6.000000   
764     3.428571        6.166667       2.500000          5.714286   
765     5.000000        5.666667       4.833333          5.571429   
766     5.428571        5.857143       4.142857          6.571429   
767     4.571429        6.142857       4.714286          5.857143   
768     4.857143        6.500000       5.285714          6.285714   
769          NaN             NaN            NaN               NaN   
770     5.000000        6.000000       5.428571          4.428571   
771     4.285714        6.142857       5.428571          6.571429   
772     4.000000        5.285714       5.142857          5.285714   
773     4.428571        6.714286       4.833333          6.285714   
774     5.142857        7.000000       5.833333          6.285714   
775     5.428571        6.857143       5.200000          6.285714   
776     4.857143        6.571429       6.428571          6.571429   
777     6.285714        6.571429       4.714286          5.571429   
778     5.285714        7.000000       4.000000          6.142857   
779     6.166667        7.000000       7.000000          6.571429   

     IBQ_Fall_React  IBQ_Cuddliness  IBQ_Percept_Sens  IBQ_Sadness  \
0               NaN             NaN               NaN          NaN   
1               NaN             NaN               NaN          NaN   
2               NaN             NaN               NaN          NaN   
3               NaN             NaN               NaN          NaN   
4               NaN             NaN               NaN          NaN   
5               NaN             NaN               NaN          NaN   
6               NaN             NaN               NaN          NaN   
7               NaN             NaN               NaN          NaN   
8               NaN             NaN               NaN          NaN   
9               NaN             NaN               NaN          NaN   
10              NaN             NaN               NaN          NaN   
11              NaN             NaN               NaN          NaN   
12              NaN             NaN               NaN          NaN   
13              NaN             NaN               NaN          NaN   
14              NaN             NaN               NaN          NaN   
15              NaN             NaN               NaN          NaN   
16              NaN             NaN               NaN          NaN   
17              NaN             NaN               NaN          NaN   
18              NaN             NaN               NaN          NaN   
19              NaN             NaN               NaN          NaN   
20              NaN             NaN               NaN          NaN   
21              NaN             NaN               NaN          NaN   
22              NaN             NaN               NaN          NaN   
23              NaN             NaN               NaN          NaN   
24              NaN             NaN               NaN          NaN   
25              NaN             NaN               NaN          NaN   
26              NaN             NaN               NaN          NaN   
27              NaN             NaN               NaN          NaN   
28              NaN             NaN               NaN          NaN   
29              NaN             NaN               NaN          NaN   
..              ...             ...               ...          ...   
750        5.000000        6.166667          4.666667     3.500000   
751        5.666667        5.666667          3.333333     2.833333   
752        6.166667        6.400000          2.400000     3.000000   
753        5.166667        6.166667          2.333333     2.333333   
754        4.166667        6.666667          3.600000     4.166667   
755             NaN             NaN               NaN          NaN   
756        5.166667        5.500000          2.500000     4.500000   
757        6.166667        4.166667          2.500000     3.166667   
758        5.600000        5.833333          5.666667     4.250000   
759        5.500000        5.000000          4.500000     3.500000   
760        6.166667        5.500000          6.500000     3.166667   
761        6.833333        5.833333          3.500000     5.000000   
762        4.833333        5.666667          2.000000     3.166667   
763        4.833333        4.166667          4.000000     4.500000   
764        5.500000        5.400000          5.250000     3.500000   
765        5.500000        5.666667          4.166667     3.166667   
766        5.666667        4.833333          3.000000     3.666667   
767        5.000000        4.500000          2.500000     3.666667   
768        6.333333        6.500000          4.000000     2.500000   
769             NaN             NaN               NaN          NaN   
770        5.000000        5.333333          5.000000     4.000000   
771        6.166667        5.333333          4.750000     2.500000   
772        4.500000        5.500000          5.000000     4.000000   
773        5.600000        6.000000          3.000000     2.500000   
774        4.333333        5.500000          5.000000     4.833333   
775        6.666667        6.200000          6.000000     3.000000   
776        6.666667        5.333333          5.500000     2.666667   
777        3.333333        5.333333          5.333333     5.000000   
778        4.166667        5.833333          2.000000     4.333333   
779        6.166667        4.833333          4.600000     2.333333   

     IBQ_Approach  IBQ_Vocal_React  IBQ_SURGENCY  IBQ_NEGATIVE_EMO  \
0             NaN              NaN      4.629412          2.337104   
1             NaN              NaN      3.950980          2.675987   
2             NaN              NaN      4.606335          2.387500   
3             NaN              NaN      4.662745          3.052632   
4             NaN              NaN      4.164706          2.518421   
5             NaN              NaN      3.862745          2.646429   
6             NaN              NaN      4.913725          4.506250   
7             NaN              NaN      4.784314          3.625000   
8             NaN              NaN      4.847059          2.466667   
9             NaN              NaN      4.986275          2.781250   
10            NaN              NaN      4.991071          3.000000   
11            NaN              NaN      4.477679          3.183333   
12            NaN              NaN      5.537815          2.150000   
13            NaN              NaN      4.884314          3.675000   
14            NaN              NaN      3.150980          3.368421   
15            NaN              NaN      5.566434          3.281250   
16            NaN              NaN      5.025490          2.417544   
17            NaN              NaN      4.239496          2.993927   
18            NaN              NaN      4.248869          3.275000   
19            NaN              NaN      4.919608          2.300000   
20            NaN              NaN      5.489583          4.125000   
21            NaN              NaN      4.352083          3.483333   
22            NaN              NaN      4.566667          2.818750   
23            NaN              NaN      4.710084          2.066667   
24            NaN              NaN      3.792157          2.832143   
25            NaN              NaN      4.571429          3.032143   
26            NaN              NaN      4.990950          2.152778   
27            NaN              NaN      4.105882          2.931250   
28            NaN              NaN      3.074661          2.275000   
29            NaN              NaN      3.989496          3.396761   
..            ...              ...           ...               ...   
750      6.000000         5.142857      5.063492          4.148810   
751      5.166667         3.500000      4.452381          3.172619   
752      5.166667         5.833333      4.900000          2.970238   
753      5.166667         3.166667      3.753968          2.630952   
754      6.333333         4.285714      4.846032          3.910714   
755           NaN              NaN           NaN               NaN   
756      5.833333         5.285714      4.888889          3.819048   
757      6.200000         4.285714      4.116667          2.478571   
758      4.800000         4.142857      4.958730          3.441071   
759      5.333333         6.000000      5.234127          4.000000   
760      6.166667         5.428571      5.845238          3.321429   
761      4.400000         3.571429      4.102381          3.113095   
762      5.400000         4.000000      4.447619          3.779762   
763      5.500000         5.666667      5.170635          4.330952   
764      6.166667         5.142857      5.049603          3.114286   
765      6.000000         5.428571      5.043651          2.517857   
766      6.333333         5.714286      5.365079          3.589286   
767      6.000000         4.857143      4.797619          3.738095   
768      6.333333         4.833333      5.015873          2.148810   
769           NaN              NaN           NaN               NaN   
770      5.166667         5.142857      5.051587          3.255952   
771      5.333333         5.166667      5.089286          2.398810   
772      4.200000         4.571429      4.342857          3.653571   
773      7.000000         5.833333      5.353175          2.432143   
774      6.333333         5.142857      5.555556          4.404762   
775      4.666667         3.857143      5.015873          2.054762   
776      6.500000         5.285714      5.285714          2.107143   
777      6.500000         6.571429      6.186508          4.994048   
778      6.000000         6.714286      4.952381          4.053571   
779      6.666667         6.571429      5.953175          2.636905   

     IBQ_EMOTION_REGUL  
0             5.227273  
1             3.505556  
2             4.010101  
3             3.590909  
4             4.031818  
5             5.388889  
6             4.050000  
7             5.272727  
8             4.363636  
9             3.733333  
10            3.500000  
11            3.909091  
12            3.945455  
13            4.818182  
14            2.500000  
15            3.888889  
16            4.545455  
17            3.390909  
18            4.439394  
19            3.479798  
20            4.181818  
21            4.616162  
22            4.846591  
23            3.727273  
24            4.100000  
25            3.646465  
26            3.772727  
27            3.868182  
28            3.565657  
29            3.272727  
..                 ...  
750           5.202381  
751           4.351190  
752           5.314286  
753           4.910714  
754           4.791667  
755                NaN  
756           5.321429  
757           4.232143  
758           5.301190  
759           4.571429  
760           5.717857  
761           4.683333  
762           4.988095  
763           4.791667  
764           4.111905  
765           4.642857  
766           4.428571  
767           4.267857  
768           5.642857  
769                NaN  
770           4.755952  
771           5.416667  
772           4.690476  
773           4.988095  
774           5.363095  
775           5.254762  
776           6.083333  
777           5.029762  
778           4.702381  
779           5.392857  

[780 rows x 19 columns]
In [7]:
# Get the age informatin from their subject IDs
df['Age'] = [int(df['subj'][i].split('a')[1].split('f')[0]) for i in np.arange(0, df.shape[0])]
print(df.columns)
Index(['subj', 'Sex', 'IBQ_Activity', 'IBQ_Distress', 'IBQ_Fear',
       'IBQ_Orienting', 'IBQ_Smiling', 'IBQ_High_Pleas', 'IBQ_Low_Pleas',
       'IBQ_Soothability', 'IBQ_Fall_React', 'IBQ_Cuddliness',
       'IBQ_Percept_Sens', 'IBQ_Sadness', 'IBQ_Approach', 'IBQ_Vocal_React',
       'IBQ_SURGENCY', 'IBQ_NEGATIVE_EMO', 'IBQ_EMOTION_REGUL', 'Age'],
      dtype='object')
In [8]:
# check if the data type of the IBQ variables is float. 
# In the new csv file, the empty cells have a space in them, which resulted in the variables being read in as objects
# because they are a mix of strings (i.e., " ") and values
print(df['IBQ_Distress'].dtype)
float64
In [9]:
dfbackup = df.copy()
print(dfbackup.shape)
(780, 20)
In [10]:
# Delete the subjects with old IBQ questionnaires
df = dfbackup.copy()
df['old_IBQ'] = np.isnan(df['IBQ_Fall_React'].values) & np.isnan(df['IBQ_Cuddliness'].values) & np.isnan(df['IBQ_Percept_Sens'].values)
df = df[df['old_IBQ'] == False]
print(df.shape)
print(df)
(700, 21)
         subj  Sex  IBQ_Activity  IBQ_Distress  IBQ_Fear  IBQ_Orienting  \
31    s033a7f    1      5.285714      4.142857  2.166667       2.666667   
33    s035a7f    0      6.000000      3.857143  2.166667       5.666667   
35   s037a12f    1      3.142857      4.571429  4.500000       2.500000   
36    s038a7a    1      4.142857      3.500000  2.750000       3.166667   
37    s039a7f    0      2.000000      2.571429  1.666667       6.000000   
38   s040a12a    0      4.285714      4.571429  2.833333       3.666667   
39    s041a5f    1      3.000000      3.714286  1.833333       4.666667   
40   s042a12a    1      4.142857      3.833333  2.600000       4.400000   
41   s043a12f    0      4.571429      3.142857  1.000000       2.400000   
42    s044a5a    0      3.285714      3.000000  2.166667       4.333333   
43    s045a5f    1      1.714286      2.500000  1.400000       4.333333   
44   s046a12a    1      4.571429      5.600000  2.000000       2.166667   
45   s047a12f    1      6.000000      4.000000  2.333333       4.000000   
46   s048a12a    0      5.142857      5.000000  4.500000       3.666667   
47    s049a5a    0      5.714286      4.428571  1.833333       4.000000   
48    s050a5f    0      2.857143      3.857143  1.166667       5.333333   
49   s052a12f    1      3.857143      4.333333  2.750000       5.600000   
50    s053a5a    1      3.000000      2.428571  2.333333       6.000000   
51    s054a7f    1      4.714286      3.857143  3.166667       3.000000   
52    s055a7a    0      4.285714      4.142857  2.000000       6.500000   
53    s056a5f    0      4.285714      3.428571  2.166667       5.333333   
54   s057a12a    0      2.000000      3.142857  2.400000       5.666667   
55   s058a12a    0      3.285714      3.166667  1.400000       4.333333   
56    s059a7f    0      4.571429      4.571429  1.666667       3.800000   
58   s061a12f    1      2.714286      2.428571  4.333333       3.000000   
59   s062a12a    1      5.200000      3.714286  2.833333       1.666667   
60    s063a5f    0      3.166667      5.428571  1.200000       3.000000   
61   s064a12a    0      5.428571      2.285714  2.666667       3.333333   
62   s066a12a    1      5.142857      5.714286  3.333333       3.833333   
63   s067a12f    1      4.142857      2.571429  1.666667       3.500000   
..        ...  ...           ...           ...       ...            ...   
747  s852a12f    0      4.428571      5.285714  3.333333       3.833333   
749  s854a12f    0      5.428571      4.000000  2.250000       2.666667   
750  s855a12f    1      4.571429      5.428571  4.666667       3.500000   
751   s856a7f    0      5.000000      3.857143  3.666667       2.166667   
752  s857a12f    0      4.000000      3.714286  3.333333       3.000000   
753   s858a7f    1      2.714286      3.857143  1.500000       3.333333   
754   s859a7f    1      5.000000      4.142857  3.500000       1.833333   
756  s861a12f    0      4.285714      5.142857  2.800000       3.500000   
757  s862a12f    1      4.857143      3.714286  1.200000       2.333333   
758  s863a12f    1      4.142857      4.714286  2.400000       3.000000   
759  s864a12f    1      5.285714      5.000000  5.000000       3.000000   
760  s865a12f    0      5.142857      4.285714  4.000000       4.800000   
761  s866a12f    0      4.857143      4.285714  2.000000       2.500000   
762   s867a7f    0      3.142857      4.285714  4.500000       4.000000   
763  s868a12f    1      6.142857      5.857143  3.800000       3.000000   
764  s869a12f    0      4.142857      4.857143  1.600000       2.833333   
765  s870a12f    0      4.000000      2.571429  1.833333       2.500000   
766  s871a12f    1      5.857143      4.857143  3.500000       2.166667   
767  s872a12f    0      4.714286      4.285714  4.000000       2.000000   
768  s873a12f    0      3.571429      2.428571  2.000000       4.500000   
770   s875a7f    0      4.000000      3.857143  2.166667       3.833333   
771   s876a7f    1      4.857143      2.428571  2.833333       4.333333   
772   s877a7f    1      3.000000      4.714286  2.400000       2.833333   
773   s878a7f    1      5.142857      3.428571  1.400000       2.833333   
774  s879a12f    0      4.714286      5.285714  3.833333       3.833333   
775  s880a12f    0      3.285714      2.285714  1.600000       3.333333   
776  s881a12f    0      3.000000      3.428571  1.000000       6.000000   
777  s882a12f    0      5.857143      5.142857  5.166667       4.500000   
778  s883a12f    1      2.714286      4.714286  3.333333       2.833333   
779  s884a12f    1      4.714286      2.714286  3.666667       3.166667   

     IBQ_Smiling  IBQ_High_Pleas  IBQ_Low_Pleas  IBQ_Soothability  ...  \
31      4.714286        5.428571       4.142857          5.285714  ...   
33      5.428571        7.000000       6.428571          6.000000  ...   
35      6.285714        7.000000       5.800000          6.571429  ...   
36      4.571429        6.285714       5.857143          5.857143  ...   
37      4.571429        6.428571       6.714286          7.000000  ...   
38      5.428571        6.000000       5.600000          5.857143  ...   
39      4.000000        5.200000       5.500000          6.428571  ...   
40      5.571429        6.857143       5.666667          6.000000  ...   
41      3.285714        5.500000       5.500000          4.571429  ...   
42      4.142857        6.285714       4.500000          6.600000  ...   
43      5.333333        6.200000       5.750000          6.000000  ...   
44      4.666667        6.000000       3.000000          6.142857  ...   
45      5.857143        5.857143       4.600000          5.857143  ...   
46      5.285714        6.666667       5.000000          5.000000  ...   
47      5.428571        6.857143       6.428571          5.285714  ...   
48      4.000000        5.285714       5.714286          4.428571  ...   
49      4.714286        5.666667       5.333333          6.000000  ...   
50      6.000000        6.000000       6.000000          6.571429  ...   
51      3.142857        6.400000       4.714286          6.142857  ...   
52      4.000000        6.600000       5.571429          5.142857  ...   
53      5.428571        6.000000       6.142857          6.000000  ...   
54      6.142857        7.000000       6.833333          6.000000  ...   
55      5.142857        7.000000       6.000000          6.428571  ...   
56      5.142857        7.000000       6.142857          6.714286  ...   
58      5.571429        6.857143       5.857143          6.142857  ...   
59      4.857143        5.500000       3.833333          5.857143  ...   
60      3.200000        4.000000       5.000000          5.400000  ...   
61      3.571429        6.142857       5.500000          5.428571  ...   
62      3.428571        4.833333       4.333333          4.666667  ...   
63      3.428571        5.571429       3.857143          4.666667  ...   
..           ...             ...            ...               ...  ...   
747     5.428571        6.000000       4.428571          5.666667  ...   
749     4.428571        7.000000       5.166667          6.142857  ...   
750     3.571429        6.428571       5.285714          5.857143  ...   
751     3.714286        6.000000       3.857143          5.714286  ...   
752     5.714286        6.285714       6.000000          5.857143  ...   
753     3.857143        5.285714       4.571429          5.571429  ...   
754     4.142857        5.714286       5.000000          5.666667  ...   
756     5.428571        6.000000       6.000000          6.285714  ...   
757     1.857143        5.000000       4.000000          6.428571  ...   
758     4.000000        7.000000       5.800000          6.571429  ...   
759     5.285714        5.000000       5.000000          5.285714  ...   
760     5.000000        6.833333       5.857143          6.714286  ...   
761     4.142857        4.142857       4.600000          5.800000  ...   
762     5.571429        6.571429       6.142857          4.142857  ...   
763     4.000000        5.714286       6.000000          6.000000  ...   
764     3.428571        6.166667       2.500000          5.714286  ...   
765     5.000000        5.666667       4.833333          5.571429  ...   
766     5.428571        5.857143       4.142857          6.571429  ...   
767     4.571429        6.142857       4.714286          5.857143  ...   
768     4.857143        6.500000       5.285714          6.285714  ...   
770     5.000000        6.000000       5.428571          4.428571  ...   
771     4.285714        6.142857       5.428571          6.571429  ...   
772     4.000000        5.285714       5.142857          5.285714  ...   
773     4.428571        6.714286       4.833333          6.285714  ...   
774     5.142857        7.000000       5.833333          6.285714  ...   
775     5.428571        6.857143       5.200000          6.285714  ...   
776     4.857143        6.571429       6.428571          6.571429  ...   
777     6.285714        6.571429       4.714286          5.571429  ...   
778     5.285714        7.000000       4.000000          6.142857  ...   
779     6.166667        7.000000       7.000000          6.571429  ...   

     IBQ_Cuddliness  IBQ_Percept_Sens  IBQ_Sadness  IBQ_Approach  \
31         6.166667          2.250000     4.400000      4.800000   
33         4.333333          3.500000     2.800000      6.600000   
35         6.000000          4.500000     3.500000      5.500000   
36         6.166667          4.500000     3.166667      5.000000   
37         7.000000          1.000000     1.666667      5.000000   
38         5.000000          6.000000     3.500000      6.500000   
39         6.833333          6.000000     3.666667      4.666667   
40         5.333333          5.400000     2.800000      6.666667   
41         5.500000          2.500000     2.666667      5.500000   
42         5.333333          3.250000     2.750000      5.000000   
43         6.833333               NaN     2.333333      5.400000   
44         4.000000          2.000000     2.666667      5.833333   
45         5.000000          6.500000     4.833333      6.166667   
46         5.833333          5.000000     4.666667      5.833333   
47         5.333333          5.000000     4.333333      7.000000   
48         6.666667          1.666667     2.666667      5.000000   
49         4.000000          3.666667     2.500000      5.666667   
50         6.000000          4.166667     2.833333      5.000000   
51         6.000000          1.500000     4.666667      3.333333   
52         5.333333          3.666667     3.000000      5.833333   
53         6.500000          6.000000     3.500000      6.000000   
54         6.166667          4.833333     2.166667      6.333333   
55         5.166667          6.000000     2.333333      6.833333   
56         7.000000          5.600000     3.666667      6.200000   
58         6.333333          3.000000     3.833333      5.166667   
59         5.400000          3.200000     4.166667      5.333333   
60         6.000000               NaN     5.333333      4.000000   
61         2.833333          6.000000     3.166667      5.500000   
62         4.833333          5.333333     4.833333      4.666667   
63         5.400000          4.000000     3.666667      5.333333   
..              ...               ...          ...           ...   
747        5.500000          5.000000     4.000000      6.500000   
749        5.333333          6.000000     3.333333      6.666667   
750        6.166667          4.666667     3.500000      6.000000   
751        5.666667          3.333333     2.833333      5.166667   
752        6.400000          2.400000     3.000000      5.166667   
753        6.166667          2.333333     2.333333      5.166667   
754        6.666667          3.600000     4.166667      6.333333   
756        5.500000          2.500000     4.500000      5.833333   
757        4.166667          2.500000     3.166667      6.200000   
758        5.833333          5.666667     4.250000      4.800000   
759        5.000000          4.500000     3.500000      5.333333   
760        5.500000          6.500000     3.166667      6.166667   
761        5.833333          3.500000     5.000000      4.400000   
762        5.666667          2.000000     3.166667      5.400000   
763        4.166667          4.000000     4.500000      5.500000   
764        5.400000          5.250000     3.500000      6.166667   
765        5.666667          4.166667     3.166667      6.000000   
766        4.833333          3.000000     3.666667      6.333333   
767        4.500000          2.500000     3.666667      6.000000   
768        6.500000          4.000000     2.500000      6.333333   
770        5.333333          5.000000     4.000000      5.166667   
771        5.333333          4.750000     2.500000      5.333333   
772        5.500000          5.000000     4.000000      4.200000   
773        6.000000          3.000000     2.500000      7.000000   
774        5.500000          5.000000     4.833333      6.333333   
775        6.200000          6.000000     3.000000      4.666667   
776        5.333333          5.500000     2.666667      6.500000   
777        5.333333          5.333333     5.000000      6.500000   
778        5.833333          2.000000     4.333333      6.000000   
779        4.833333          4.600000     2.333333      6.666667   

     IBQ_Vocal_React  IBQ_SURGENCY  IBQ_NEGATIVE_EMO  IBQ_EMOTION_REGUL  Age  \
31          5.000000      4.579762          3.385714           4.565476    7   
33          4.833333      5.560317          2.789286           5.607143    7   
35          4.285714      5.119048          3.851190           5.217857   12   
36          5.714286      5.035714          2.812500           5.261905    7   
37          5.714286      4.119048          1.788690           6.678571    7   
38          6.714286      5.821429          3.184524           5.030952   12   
39          4.000000      4.477778          2.761905           5.857143    5   
40          5.166667      5.634127          2.683333           5.350000   12   
41          5.166667      4.420635          2.152381           4.492857   12   
42          3.714286      4.279762          2.479167           5.191667    5   
43          3.857143      4.500952          2.008333           5.729167    5   
44          5.333333      4.734127          3.566667           3.827381   12   
45          5.571429      5.992063          3.291667           4.864286   12   
46          5.285714      5.535714          4.500000           4.875000   12   
47          6.285714      6.047619          3.107143           5.261905    5   
48          4.333333      3.857143          2.422619           5.535714    5   
49          6.000000      4.928571          3.312500           5.233333   12   
50          5.500000      4.944444          2.273810           6.142857    5   
51          5.000000      4.015079          3.860119           4.964286    7   
52          4.000000      4.730952          3.410714           5.636905    7   
53          6.142857      5.642857          2.815476           5.994048    5   
54          6.571429      5.480159          2.635714           6.166667   12   
55          6.571429      5.805556          2.058333           5.482143   12   
56          4.833333      5.557937          3.226190           5.914286    7   
58          5.285714      4.765873          3.107143           5.333333   12   
59          5.400000      4.915079          3.428571           4.189286   12   
60          4.000000      3.673333          4.282143           4.850000    5   
61          5.285714      5.321429          2.821429           4.273810   12   
62          4.571429      4.662698          4.220238           4.416667   12   
63          3.714286      4.365079          2.684524           4.355952   12   
..               ...           ...               ...                ...  ...   
747         5.285714      5.440476          4.113095           4.857143   12   
749         6.142857      5.944444          2.979167           4.827381   12   
750         5.142857      5.063492          4.148810           5.202381   12   
751         3.500000      4.452381          3.172619           4.351190    7   
752         5.833333      4.900000          2.970238           5.314286   12   
753         3.166667      3.753968          2.630952           4.910714    7   
754         4.285714      4.846032          3.910714           4.791667    7   
756         5.285714      4.888889          3.819048           5.321429   12   
757         4.285714      4.116667          2.478571           4.232143   12   
758         4.142857      4.958730          3.441071           5.301190   12   
759         6.000000      5.234127          4.000000           4.571429   12   
760         5.428571      5.845238          3.321429           5.717857   12   
761         3.571429      4.102381          3.113095           4.683333   12   
762         4.000000      4.447619          3.779762           4.988095    7   
763         5.666667      5.170635          4.330952           4.791667   12   
764         5.142857      5.049603          3.114286           4.111905   12   
765         5.428571      5.043651          2.517857           4.642857   12   
766         5.714286      5.365079          3.589286           4.428571   12   
767         4.857143      4.797619          3.738095           4.267857   12   
768         4.833333      5.015873          2.148810           5.642857   12   
770         5.142857      5.051587          3.255952           4.755952    7   
771         5.166667      5.089286          2.398810           5.416667    7   
772         4.571429      4.342857          3.653571           4.690476    7   
773         5.833333      5.353175          2.432143           4.988095    7   
774         5.142857      5.555556          4.404762           5.363095   12   
775         3.857143      5.015873          2.054762           5.254762   12   
776         5.285714      5.285714          2.107143           6.083333   12   
777         6.571429      6.186508          4.994048           5.029762   12   
778         6.714286      4.952381          4.053571           4.702381   12   
779         6.571429      5.953175          2.636905           5.392857   12   

     old_IBQ  
31     False  
33     False  
35     False  
36     False  
37     False  
38     False  
39     False  
40     False  
41     False  
42     False  
43     False  
44     False  
45     False  
46     False  
47     False  
48     False  
49     False  
50     False  
51     False  
52     False  
53     False  
54     False  
55     False  
56     False  
58     False  
59     False  
60     False  
61     False  
62     False  
63     False  
..       ...  
747    False  
749    False  
750    False  
751    False  
752    False  
753    False  
754    False  
756    False  
757    False  
758    False  
759    False  
760    False  
761    False  
762    False  
763    False  
764    False  
765    False  
766    False  
767    False  
768    False  
770    False  
771    False  
772    False  
773    False  
774    False  
775    False  
776    False  
777    False  
778    False  
779    False  

[700 rows x 21 columns]

Imputing missing values with Multivariate Imputation by Chained Equations (mice)

In [11]:
df[IBQ_columns] = impyute.mice(df[IBQ_columns].values, loop=5000)
print(df.shape)
(700, 21)
In [12]:
# check if there is still missing value (nan) for each variable
df.isnull().any(axis=0)
Out[12]:
subj                 False
Sex                  False
IBQ_Activity         False
IBQ_Distress         False
IBQ_Fear             False
IBQ_Orienting        False
IBQ_Smiling          False
IBQ_High_Pleas       False
IBQ_Low_Pleas        False
IBQ_Soothability     False
IBQ_Fall_React       False
IBQ_Cuddliness       False
IBQ_Percept_Sens     False
IBQ_Sadness          False
IBQ_Approach         False
IBQ_Vocal_React      False
IBQ_SURGENCY         False
IBQ_NEGATIVE_EMO     False
IBQ_EMOTION_REGUL    False
Age                  False
old_IBQ              False
dtype: bool

This was JB's code to use "Expectation Maximization (EM)" for imputation.

df[IBQ_columns] = impyute.em(df[IBQ_columns].values, loop=5000)

Visualizing the distributions of the behavioural variables

In [17]:
IBQ_scales = [
            'Approach', 
            'Vocal_React',
            'High_Pleas',
            'Smiling',
            'Activity', 
            'Percept_Sens', 
            'Sadness',
            'Distress',
            'Fear',
            'Fall_React',
            'Low_Pleas',
            'Cuddliness',
            'Orienting',
            'Soothability']
IBQ_columns = ['IBQ_' + column for column in IBQ_scales]

Functions for doing community detection and plots

In [5]:
def community_clustering(df):
    # Calculate Mahalanobis distance
    distances = pdist(df.values, metric='euclidean')
    dist_matrix = squareform(distances)

    # Building the participant by participant network
    corrmat = df.transpose().corr().values

    # Running the community clustering
    clusters, q = get_consensus_module_assignment(corrmat)
    print('Q-index: q=%.2f' % q)


    # Calculating the sorting array
    sorting_array = sorted(range(len(clusters)), key=lambda k: clusters[k])
    sorted_array = corrmat
    sorted_array = sorted_array[sorting_array, :]
    sorted_array = sorted_array[:, sorting_array]

    # Plot
    fig,(ax1, ax2) = plt.subplots(1, 2, figsize=(80*mm2inches,40*mm2inches), dpi=300)
    ax1.imshow(corrmat, vmin=-1, vmax=1, cmap='bwr', aspect='auto')
    ax1.axis('off')
    ax2.imshow(sorted_array, vmin=-1, vmax=1, cmap='bwr', aspect='auto')
    ax2.axis('off')
    plt.show()

    return clusters
In [15]:
def plot_profiles(df, clusters):
    df['group'] = clusters
    df['group'] = df['group'].astype('int')
    means = df.groupby('group').mean().values
    SE = 2*df.groupby('group').sem().values
    colours = ["#E64B35B2", "#4DBBD5B2", "#00A087B2", "#3C5488B2", "#F39B7FB2", "#8491B4B2", "#91D1C2B2", "#DC0000B2", "#7E6148B2"]
    colnames = [''.join(IBQ_column.split('_')[1:]) for IBQ_column in IBQ_columns]

    plt.figure(figsize=(50*mm2inches, 50*mm2inches), dpi=300)
    plt.errorbar(np.arange(0*0.1, means.shape[1]), means[0, :], SE[0, :], color=colours[0], linewidth=1, 
                 marker='o', markersize=2, capsize=2, elinewidth=1, markeredgewidth=2)
    plt.errorbar(np.arange(1*0.1, means.shape[1]), means[1, :], SE[1, :], color=colours[1], linewidth=1, 
                 marker='o', markersize=2, capsize=2, elinewidth=1, markeredgewidth=2)
    if len(np.unique(clusters)) > 2:
        plt.errorbar(np.arange(2*0.1, means.shape[1]), means[2, :], SE[2, :], color=colours[2], linewidth=1, 
                 marker='o', markersize=2, capsize=2, elinewidth=1, markeredgewidth=2)
    else:
        print('There are two clusters')
    
   # plt.errorbar(np.arange(2*0.1, means.shape[1]), means[2, :], SE[2, :], color=colours[2], linewidth=1, 
   #              marker='o', markersize=2, capsize=2, elinewidth=1, markeredgewidth=2)
    labels = ['C' + str(i) for i in np.arange(1, len(np.unique(df['group']))+1)]
    plt.legend(labels, bbox_to_anchor=(1.1, 1.05))
    plt.ylim([-2, 2])
    plt.axhline(0, color='k', linewidth=1, alpha=0.8)
    plt.axhline(-1, color='k', linestyle='dashed', linewidth=1, alpha=0.8)
    plt.axhline(+1, color='k', linestyle='dashed', linewidth=1, alpha=0.8)
    ax = plt.gca()
    ax.set_xticks(np.arange(0, len(colnames)))
    ax.set_xticklabels(colnames, rotation=90)
    
    # set the distance between tick labels and axes
    ax.tick_params(axis='both', which='major', pad=0)
    
    # WX changed the font of the labels because they appear to be crowded on his monitor.
    indexes = range(1,len(IBQ_scales)+1,1)
    for tick, index in zip(ax.xaxis.get_major_ticks(),indexes):
            tick.label.set_fontsize(6) 
            if index<=6:
                tick.label.set_color('navy')
            elif index >= 11:
                tick.label.set_color('b')
            else:
                tick.label.set_color('magenta')
    
    
    

Community clustering on the original data. It is a test for the programs, but it does not take care of age and sex that both should have an impact on child temperament.

In [ ]:
temp_df = df[np.hstack(['subj', IBQ_columns, 'Age'])].copy()
temp_df[IBQ_columns] = temp_df[IBQ_columns].copy().apply(zscore)
clusters = community_clustering(temp_df[IBQ_columns])
In [18]:
plot_profiles(temp_df[IBQ_columns], clusters)

Community clustering when age is regressed

Calculate the residuals for each IBQ variables with the age being the regressor

In [19]:
temp_df = df[np.hstack(['subj', IBQ_columns, 'Age','Sex'])].copy()
for column in IBQ_columns: 
    temp_df[column] = smf.ols(column + '~ C(Age)', data=temp_df).fit().resid.values
temp_df[IBQ_columns] = temp_df[IBQ_columns].copy().apply(zscore)

Do community detection with the "age-regressed" data

In [20]:
clusters = community_clustering(temp_df[IBQ_columns])
Q-index: q=0.31
In [21]:
plot_profiles(temp_df[IBQ_columns], clusters)
There are two clusters

Save the clustering results to a .csv file

In [42]:
temp_df['clusters_1yrs'] = clusters
temp_df[['subj', 'Age','Sex','clusters_1yrs']].to_csv('../data/derived/clusters_1yrs_AgeRegressed.csv')

Do Community Detection for male and female infants separately

In [22]:
# boy(0)
temp_df = df[np.hstack(['subj', IBQ_columns, 'Age','Sex'])].copy()
temp_df_b = temp_df[temp_df['Sex'].isin([0])].copy()
for column in IBQ_columns: 
    temp_df_b[column] = smf.ols(column + '~ C(Age)', data=temp_df_b).fit().resid.values
temp_df_b[IBQ_columns] = temp_df_b[IBQ_columns].copy().apply(zscore)
print(temp_df_b.shape)
(380, 17)
In [ ]:
# temp_df_b = temp_df[temp_df['Sex'].isin([0])].copy()
# print(temp_df_b.shape)
In [23]:
clusters = community_clustering(temp_df_b[IBQ_columns])
Q-index: q=0.31
In [24]:
plot_profiles(temp_df_b[IBQ_columns], clusters)
In [25]:
# save the output
temp_df_b['clusters_1yrs'] = clusters
temp_df_b[['subj', 'Age','Sex','clusters_1yrs']].to_csv('../data/derived/clusters_1yrs_AgeRegressed_boys.csv')
In [26]:
# girl(1)
temp_df = df[np.hstack(['subj', IBQ_columns, 'Age','Sex'])].copy()
temp_df_g = temp_df[temp_df['Sex'].isin([1])].copy()
for column in IBQ_columns: 
    temp_df_g[column] = smf.ols(column + '~ C(Age)', data=temp_df_g).fit().resid.values
temp_df_g[IBQ_columns] = temp_df_g[IBQ_columns].copy().apply(zscore)
print(temp_df_g.shape)
(320, 17)
In [27]:
clusters = community_clustering(temp_df_g[IBQ_columns])
plot_profiles(temp_df_g[IBQ_columns], clusters)
Q-index: q=0.32
There are two clusters
In [28]:
# save the output
temp_df_g['clusters_1yrs'] = clusters
temp_df_g[['subj', 'Age','Sex','clusters_1yrs']].to_csv('../data/derived/clusters_1yrs_AgeRegressed_girls.csv')

Community Detection for 2 and 3 yrs ECBQ data

define the ECBQ variables

In [6]:
EBQ_scales = [
        'Impulsive',
        'Activity',
        'High_Pleas',
        'Sociability',
        'Pos_Anticipate',
        'Discomfort',
        'Fear',
        'Motor',
        'Sadness',
        'Percept_Sens',
        'Shyness',
        'Soothability',
        'Frustration',
        'Inhib_Cntrl',
        'Attn_Shift',
        'Low_Pleas',
        'Cuddliness',
        'Attn_Focus'
]

read in the ECBQ data for 2 yrs

In [6]:
df = pd.read_csv('../data/raw/TempAndQuestions_all.csv')
EBQ_columns = ['ECBQ_' + column + '_2yr' for column in EBQ_scales]
columns_to_use = np.hstack([['subj', 'Sex'], EBQ_columns])
# columns_to_use = np.hstack([['subj'], EBQ_columns])
df = df[columns_to_use].copy()
print('Total included: %i' % len(df))
print(df.shape)
Total included: 780
(780, 20)

checking missing data

In [7]:
df['missing'] = df[EBQ_columns].isnull().sum(axis=1) > 2 # if missing more than 2 ECBQ variables then no interpolation
print('Number of cases with missing values: %i' % sum(df['missing']))

df = df[df['missing'] == False]
print('Total included: %i' % len(df))
print(df.shape)
Number of cases with missing values: 258
Total included: 522
(522, 21)

Interpolation the missing values

In [8]:
df[EBQ_columns] = impyute.mice(df[EBQ_columns].values, loop=5000)
# df = df.dropna()
print(df)
# print(df[EBQ_columns]) 
         subj  Sex  ECBQ_Impulsive_2yr  ECBQ_Activity_2yr  \
0     s001a5f    1                5.25           5.375000   
1     s002a5a    0                4.00           5.142857   
2     s003a5f    0                5.00           5.625000   
6     s008a7a    1                4.00           4.750000   
8     s010a7a    0                5.75           3.375000   
9     s011a7f    1                2.50           5.250000   
10    s012a7a    0                4.75           4.875000   
11    s013a7f    0                3.75           5.750000   
12    s014a7a    0                4.25           5.125000   
16    s018a7a    0                5.00           6.000000   
17    s019a7f    1                4.75           3.375000   
19    s021a7f    0                5.50           5.375000   
20    s022a7a    1                3.50           5.125000   
22    s024a7f    0                3.50           4.000000   
25    s027a7f    0                3.00           3.625000   
26    s028a7a    1                5.00           3.750000   
28    s030a7a    1                2.75           4.875000   
29    s031a7f    0                3.50           6.125000   
30    s032a7a    0                5.75           5.750000   
32    s034a5a    1                5.00           5.500000   
33    s035a7f    0                2.75           6.125000   
34   s036a12a    0                3.75           4.625000   
35   s037a12f    1                3.50           3.625000   
37    s039a7f    0                5.25           5.500000   
38   s040a12a    0                5.00           4.875000   
39    s041a5f    1                3.00           4.625000   
40   s042a12a    1                5.25           3.250000   
41   s043a12f    0                4.25           5.250000   
42    s044a5a    0                4.75           6.750000   
43    s045a5f    1                3.50           4.500000   
..        ...  ...                 ...                ...   
739   s844a7f    1                4.50           5.625000   
740   s845a7f    1                4.00           5.250000   
741   s846a7f    1                2.50           4.500000   
742  s847a12f    0                5.75           5.125000   
744   s849a7f    0                4.00           4.250000   
745  s850a12f    0                4.00           4.500000   
746  s851a12f    1                5.00           5.375000   
749  s854a12f    0                4.75           4.750000   
750  s855a12f    1                4.00           3.500000   
752  s857a12f    0                5.25           4.000000   
753   s858a7f    1                4.25           4.125000   
754   s859a7f    1                2.50           5.625000   
756  s861a12f    0                5.75           4.750000   
758  s863a12f    1                4.25           4.750000   
759  s864a12f    1                3.50           5.625000   
760  s865a12f    0                4.75           6.375000   
761  s866a12f    0                4.00           4.375000   
762   s867a7f    0                4.75           5.125000   
763  s868a12f    1                4.75           5.500000   
764  s869a12f    0                4.50           4.625000   
765  s870a12f    0                3.25           4.250000   
766  s871a12f    1                2.50           4.875000   
771   s876a7f    1                3.75           4.875000   
773   s878a7f    1                5.00           5.750000   
774  s879a12f    0                2.25           5.625000   
775  s880a12f    0                3.00           2.000000   
776  s881a12f    0                3.75           4.375000   
777  s882a12f    0                3.00           5.250000   
778  s883a12f    1                5.00           5.500000   
779  s884a12f    1                4.50           5.500000   

     ECBQ_High_Pleas_2yr  ECBQ_Sociability_2yr  ECBQ_Pos_Anticipate_2yr  \
0               5.333333              6.750000                 6.400000   
1               5.333333              6.333333                 2.666667   
2               6.000000              6.500000                 4.600000   
6               4.500000              7.000000                 6.600000   
8               4.000000              6.250000                 5.200000   
9               5.166667              6.500000                 6.600000   
10              5.166667              3.750000                 4.250000   
11              5.500000              4.750000                 5.400000   
12              4.666667              6.333333                 5.600000   
16              5.500000              5.750000                 5.500000   
17              3.833333              4.750000                 5.200000   
19              4.500000              4.750000                 4.800000   
20              4.333333              6.000000                 4.000000   
22              5.000000              5.000000                 5.200000   
25              4.500000              4.666667                 6.000000   
26              4.333333              5.250000                 5.600000   
28              3.200000              7.000000                 5.800000   
29              6.166667              6.666667                 5.666667   
30              5.833333              6.500000                 5.400000   
32              6.166667              5.500000                 4.800000   
33              5.666667              6.750000                 5.000000   
34              4.000000              7.000000                 4.400000   
35              3.500000              4.500000                 5.200000   
37              5.666667              6.500000                 6.800000   
38              5.333333              5.250000                 5.200000   
39              4.500000              6.500000                 5.000000   
40              6.500000              6.750000                 6.600000   
41              2.500000              6.000000                 6.000000   
42              6.666667              6.000000                 5.400000   
43              4.833333              6.000000                 6.400000   
..                   ...                   ...                      ...   
739             3.666667              6.750000                 6.400000   
740             4.000000              6.750000                 6.800000   
741             3.666667              4.750000                 5.800000   
742             4.333333              4.750000                 5.800000   
744             5.166667              6.000000                 5.600000   
745             5.166667              2.500000                 6.000000   
746             4.500000              5.500000                 5.800000   
749             5.166667              6.500000                 4.000000   
750             3.666667              5.250000                 5.800000   
752             5.500000              5.666667                 4.400000   
753             5.666667              6.000000                 5.600000   
754             5.333333              4.666667                 5.800000   
756             5.166667              6.666667                 6.666667   
758             4.166667              2.500000                 4.250000   
759             5.000000              5.500000                 5.000000   
760             6.833333              6.250000                 6.000000   
761             4.500000              3.333333                 5.800000   
762             3.833333              5.250000                 4.000000   
763             4.333333              7.000000                 3.800000   
764             3.833333              5.500000                 6.200000   
765             5.500000              6.250000                 5.600000   
766             4.833333              6.500000                 6.200000   
771             5.000000              4.500000                 6.000000   
773             6.166667              3.333333                 5.600000   
774             5.166667              7.000000                 6.200000   
775             4.666667              4.750000                 3.200000   
776             5.600000              6.666667                 5.400000   
777             4.666667              5.500000                 6.000000   
778             6.833333              5.750000                 5.800000   
779             5.833333              5.750000                 6.000000   

     ECBQ_Discomfort_2yr  ECBQ_Fear_2yr  ECBQ_Motor_2yr  ...  \
0               2.000000       2.250000        2.166667  ...   
1               2.428571       1.125000        1.833333  ...   
2               2.000000       2.500000        2.166667  ...   
6               1.285714       1.125000        2.166667  ...   
8               1.142857       1.625000        1.500000  ...   
9               1.714286       2.375000        1.333333  ...   
10              2.500000       1.875000        3.333333  ...   
11              2.428571       1.750000        3.833333  ...   
12              1.571429       1.500000        1.666667  ...   
16              1.857143       1.375000        3.000000  ...   
17              1.571429       2.375000        1.000000  ...   
19              2.666667       1.375000        1.666667  ...   
20              1.285714       1.875000        1.166667  ...   
22              1.428571       1.250000        1.666667  ...   
25              3.571429       1.750000        2.833333  ...   
26              1.857143       2.125000        2.500000  ...   
28              2.500000       1.000000        1.666667  ...   
29              1.285714       1.500000        2.500000  ...   
30              1.571429       1.625000        1.166667  ...   
32              2.166667       2.600000        2.666667  ...   
33              1.285714       1.625000        2.333333  ...   
34              2.500000       1.750000        1.500000  ...   
35              2.571429       3.375000        1.166667  ...   
37              1.285714       1.500000        1.333333  ...   
38              1.142857       1.375000        1.166667  ...   
39              1.714286       2.125000        1.833333  ...   
40              1.428571       1.875000        1.500000  ...   
41              1.857143       1.750000        1.500000  ...   
42              2.285714       1.750000        3.166667  ...   
43              2.000000       2.250000        1.500000  ...   
..                   ...            ...             ...  ...   
739             1.571429       1.375000        1.166667  ...   
740             1.714286       1.375000        1.166667  ...   
741             1.571429       1.833333        2.000000  ...   
742             2.142857       3.000000        1.500000  ...   
744             1.428571       1.625000        1.166667  ...   
745             1.750000       3.125000        1.333333  ...   
746             3.000000       2.625000        3.000000  ...   
749             1.571429       1.750000        2.333333  ...   
750             2.714286       3.625000        1.666667  ...   
752             1.166667       1.125000        1.166667  ...   
753             2.714286       2.625000        2.333333  ...   
754             3.142857       1.625000        1.666667  ...   
756             1.428571       1.571429        1.833333  ...   
758             2.666667       2.250000        2.400000  ...   
759             1.833333       2.375000        2.833333  ...   
760             4.714286       2.750000        2.333333  ...   
761             1.857143       2.875000        1.666667  ...   
762             1.800000       2.000000        1.166667  ...   
763             2.000000       2.428571        1.833333  ...   
764             1.714286       2.250000        1.500000  ...   
765             2.666667       1.625000        2.200000  ...   
766             3.285714       4.250000        1.166667  ...   
771             2.285714       2.750000        2.000000  ...   
773             1.500000       1.625000        1.500000  ...   
774             3.285714       2.875000        2.166667  ...   
775             1.714286       2.125000        1.166667  ...   
776             2.142857       1.875000        2.250000  ...   
777             3.285714       3.500000        3.000000  ...   
778             2.285714       2.375000        2.666667  ...   
779             2.833333       2.500000        1.166667  ...   

     ECBQ_Percept_Sens_2yr  ECBQ_Shyness_2yr  ECBQ_Soothability_2yr  \
0                     4.00          3.000000                    5.8   
1                     3.50          1.666667                    7.0   
2                     4.40          2.600000                    6.0   
6                     2.80          4.000000                    5.6   
8                     1.40          1.400000                    5.8   
9                     3.80          4.000000                    6.2   
10                    4.00          3.000000                    4.2   
11                    6.40          3.400000                    4.2   
12                    4.00          2.250000                    5.2   
16                    3.40          2.400000                    5.0   
17                    2.00          3.000000                    6.0   
19                    4.00          1.400000                    6.0   
20                    2.25          2.800000                    6.2   
22                    3.40          3.600000                    4.6   
25                    3.40          5.000000                    4.6   
26                    4.80          2.400000                    5.4   
28                    4.80          3.600000                    5.8   
29                    3.40          3.000000                    3.8   
30                    3.60          4.000000                    5.8   
32                    5.40          3.200000                    3.4   
33                    5.40          3.600000                    6.4   
34                    2.80          3.800000                    3.2   
35                    2.60          4.400000                    5.4   
37                    2.60          2.200000                    6.6   
38                    4.00          1.800000                    5.4   
39                    6.00          2.200000                    6.0   
40                    3.80          4.800000                    6.4   
41                    5.00          2.000000                    5.8   
42                    4.00          3.000000                    4.8   
43                    4.40          3.400000                    5.8   
..                     ...               ...                    ...   
739                   4.40          2.800000                    6.0   
740                   3.40          1.600000                    5.4   
741                   3.40          2.800000                    5.8   
742                   4.20          3.000000                    4.8   
744                   2.80          3.000000                    6.2   
745                   3.40          3.000000                    5.8   
746                   5.60          2.800000                    4.2   
749                   2.60          2.600000                    6.2   
750                   5.00          3.000000                    5.0   
752                   2.80          2.400000                    4.6   
753                   3.80          3.000000                    4.8   
754                   4.20          4.000000                    5.2   
756                   4.25          3.000000                    5.4   
758                   6.00          4.600000                    6.0   
759                   5.40          4.800000                    5.2   
760                   5.80          3.000000                    5.4   
761                   3.00          5.000000                    4.6   
762                   3.00          4.800000                    6.0   
763                   3.60          4.800000                    4.6   
764                   4.00          3.000000                    5.0   
765                   4.75          2.200000                    6.0   
766                   2.40          5.800000                    6.0   
771                   4.60          4.800000                    5.6   
773                   4.75          2.200000                    5.0   
774                   4.00          5.200000                    5.2   
775                   3.80          3.800000                    6.2   
776                   6.40          2.800000                    5.0   
777                   5.80          4.800000                    5.2   
778                   3.40          2.600000                    4.4   
779                   5.80          5.400000                    5.0   

     ECBQ_Frustration_2yr  ECBQ_Inhib_Cntrl_2yr  ECBQ_Attn_Shift_2yr  \
0                2.000000              5.833333             5.750000   
1                2.166667              4.833333             4.333333   
2                2.666667              4.166667             5.250000   
6                3.166667              3.666667             5.500000   
8                2.833333              4.666667             5.500000   
9                3.166667              4.500000             4.625000   
10               4.666667              3.666667             3.285714   
11               2.000000              3.500000             4.375000   
12               2.666667              4.000000             4.875000   
16               3.333333              3.833333             4.750000   
17               3.666667              4.500000             4.500000   
19               4.666667              2.666667             4.166667   
20               3.333333              3.833333             4.500000   
22               3.500000              5.000000             5.125000   
25               3.500000              3.666667             4.625000   
26               2.333333              4.666667             4.500000   
28               2.333333              5.500000             6.000000   
29               4.333333              2.833333             4.125000   
30               3.000000              5.166667             5.250000   
32               4.000000              3.666667             4.250000   
33               2.666667              4.000000             5.875000   
34               3.666667              3.000000             4.500000   
35               3.666667              4.333333             4.375000   
37               4.333333              3.833333             4.875000   
38               3.333333              4.666667             5.500000   
39               2.333333              4.500000             5.125000   
40               3.333333              4.666667             6.000000   
41               2.833333              4.166667             5.625000   
42               4.500000              3.000000             4.875000   
43               2.500000              5.666667             5.285714   
..                    ...                   ...                  ...   
739              2.333333              6.333333             5.500000   
740              3.333333              5.333333             6.375000   
741              2.000000              5.000000             5.250000   
742              4.833333              3.333333             5.125000   
744              3.500000              3.666667             5.125000   
745              3.666667              3.833333             5.375000   
746              5.166667              3.000000             4.250000   
749              2.833333              4.000000             5.285714   
750              3.333333              4.500000             5.500000   
752              2.666667              3.833333             4.625000   
753              3.833333              4.200000             4.125000   
754              4.833333              3.500000             5.625000   
756              2.500000              4.800000             5.428571   
758              3.166667              4.500000             5.285714   
759              3.666667              3.500000             4.250000   
760              5.333333              3.833333             6.375000   
761              2.833333              3.000000             4.250000   
762              3.666667              5.000000             5.750000   
763              3.166667              4.166667             4.625000   
764              2.166667              5.000000             5.500000   
765              2.833333              4.833333             6.000000   
766              3.166667              5.000000             5.125000   
771              3.666667              4.000000             3.750000   
773              4.333333              5.000000             5.375000   
774              4.666667              3.600000             5.500000   
775              2.333333              5.666667             4.625000   
776              3.833333              5.000000             5.571429   
777              4.000000              3.166667             5.125000   
778              3.833333              3.166667             5.500000   
779              4.000000              4.000000             5.714286   

     ECBQ_Low_Pleas_2yr  ECBQ_Cuddliness_2yr  ECBQ_Attn_Focus_2yr  missing  
0              5.500000             6.333333             5.333333    False  
1              2.500000             3.500000             6.000000    False  
2              5.666667             4.666667             5.000000    False  
6              3.166667             5.166667             4.666667    False  
8              5.666667             6.166667             5.000000    False  
9              3.833333             4.666667             4.333333    False  
10             2.600000             2.833333             5.500000    False  
11             5.500000             5.666667             4.000000    False  
12             5.333333             4.833333             4.000000    False  
16             4.000000             4.333333             4.833333    False  
17             5.333333             5.500000             5.000000    False  
19             3.166667             3.166667             5.166667    False  
20             5.000000             5.666667             5.333333    False  
22             5.600000             6.166667             5.000000    False  
25             5.333333             5.000000             3.666667    False  
26             3.666667             5.333333             5.666667    False  
28             4.666667             5.833333             5.333333    False  
29             4.000000             4.500000             3.500000    False  
30             3.166667             5.500000             6.000000    False  
32             4.666667             3.500000             5.000000    False  
33             4.833333             4.500000             4.166667    False  
34             4.166667             4.833333             5.000000    False  
35             5.833333             6.333333             5.000000    False  
37             5.500000             6.333333             3.833333    False  
38             3.000000             4.000000             5.000000    False  
39             5.000000             5.166667             4.500000    False  
40             6.833333             5.833333             5.666667    False  
41             4.333333             5.166667             4.666667    False  
42             4.166667             5.166667             3.666667    False  
43             5.666667             6.000000             5.333333    False  
..                  ...                  ...                  ...      ...  
739            5.166667             6.166667             5.500000    False  
740            4.500000             3.666667             2.333333    False  
741            4.833333             4.666667             4.500000    False  
742            4.833333             5.833333             5.333333    False  
744            6.000000             5.666667             4.500000    False  
745            4.500000             4.666667             5.500000    False  
746            4.666667             4.000000             4.500000    False  
749            4.000000             5.000000             4.500000    False  
750            5.333333             5.500000             5.500000    False  
752            4.500000             5.500000             5.333333    False  
753            5.666667             5.833333             5.166667    False  
754            5.166667             4.500000             4.833333    False  
756            3.500000             5.000000             4.833333    False  
758            4.833333             6.333333             3.833333    False  
759            5.833333             5.166667             4.500000    False  
760            6.166667             5.166667             6.500000    False  
761            4.600000             4.166667             4.500000    False  
762            5.666667             4.166667             6.000000    False  
763            3.000000             5.166667             4.166667    False  
764            4.666667             4.666667             5.833333    False  
765            5.500000             5.833333             5.833333    False  
766            4.000000             4.500000             4.000000    False  
771            4.333333             4.666667             3.666667    False  
773            2.333333             3.333333             3.166667    False  
774            6.500000             5.000000             5.166667    False  
775            4.000000             5.400000             5.166667    False  
776            5.500000             4.333333             5.666667    False  
777            4.333333             4.833333             5.333333    False  
778            5.500000             5.833333             5.000000    False  
779            6.166667             6.333333             5.166667    False  

[522 rows x 21 columns]
In [9]:
# check if there is still missing value (nan) for each variable
df.isnull().any(axis=0)
Out[9]:
subj                       False
Sex                        False
ECBQ_Impulsive_2yr         False
ECBQ_Activity_2yr          False
ECBQ_High_Pleas_2yr        False
ECBQ_Sociability_2yr       False
ECBQ_Pos_Anticipate_2yr    False
ECBQ_Discomfort_2yr        False
ECBQ_Fear_2yr              False
ECBQ_Motor_2yr             False
ECBQ_Sadness_2yr           False
ECBQ_Percept_Sens_2yr      False
ECBQ_Shyness_2yr           False
ECBQ_Soothability_2yr      False
ECBQ_Frustration_2yr       False
ECBQ_Inhib_Cntrl_2yr       False
ECBQ_Attn_Shift_2yr        False
ECBQ_Low_Pleas_2yr         False
ECBQ_Cuddliness_2yr        False
ECBQ_Attn_Focus_2yr        False
missing                    False
dtype: bool

Community Detection Modify the plot program to change IBQ to EBQ

In [7]:
def plot_profiles(df, clusters):
    df['group'] = clusters
    df['group'] = df['group'].astype('int')
    means = df.groupby('group').mean().values
    SE = 2*df.groupby('group').sem().values
    colours = ["#E64B35B2", "#4DBBD5B2", "#00A087B2", "#3C5488B2", "#F39B7FB2", "#8491B4B2", "#91D1C2B2", "#DC0000B2", "#7E6148B2"]
   # colnames = [''.join(EBQ_column.split('_')[1:]) for EBQ_column in EBQ_columns]
    colnames= EBQ_scales.copy()
    plt.figure(figsize=(50*mm2inches, 50*mm2inches), dpi=300)
    plt.errorbar(np.arange(0*0.1, means.shape[1]), means[0, :], SE[0, :], color=colours[0], linewidth=1, 
                 marker='o', markersize=2, capsize=2, elinewidth=1, markeredgewidth=2)
    plt.errorbar(np.arange(1*0.1, means.shape[1]), means[1, :], SE[1, :], color=colours[1], linewidth=1, 
                 marker='o', markersize=2, capsize=2, elinewidth=1, markeredgewidth=2)
    if len(np.unique(clusters)) > 2:
        plt.errorbar(np.arange(2*0.1, means.shape[1]), means[2, :], SE[2, :], color=colours[2], linewidth=1, 
                 marker='o', markersize=2, capsize=2, elinewidth=1, markeredgewidth=2)
    else:
        print('There are two clusters')
    labels = ['C' + str(i) for i in np.arange(1, len(np.unique(df['group']))+1)]
    plt.legend(labels, bbox_to_anchor=(1.1, 1.05))
    plt.ylim([-2, 2])
    plt.axhline(0, color='k', linewidth=1, alpha=0.8)
    plt.axhline(-1, color='k', linestyle='dashed', linewidth=1, alpha=0.8)
    plt.axhline(+1, color='k', linestyle='dashed', linewidth=1, alpha=0.8)
    
    ax = plt.gca()
    ax.set_xticks(np.arange(0, len(colnames), 1.02))
    ax.set_xticklabels(colnames, rotation=90)
    # set the distance between tick labels and axes
    ax.tick_params(axis='both', which='major', pad=0)
    
    # WX changed the font of the labels because they appear to be crowded on his monitor. Does this change with the screen size?
    indexes = range(1,len(EBQ_scales)+1,1)
    for tick, index in zip(ax.xaxis.get_major_ticks(),indexes):
            tick.label.set_fontsize(6) 
            if index<=5:
                tick.label.set_color('navy')
            elif index >= 14:
                tick.label.set_color('b')
            else:
                tick.label.set_color('magenta')
In [14]:
# Community clustering on the data
# temp_df = df[np.hstack(['subj', EBQ_columns, 'Age'])].copy()
temp_df = df.copy();
temp_df[EBQ_columns] = temp_df[EBQ_columns].copy().apply(zscore)
clusters = community_clustering(temp_df[EBQ_columns])

nclusters = np.unique(clusters)
print('Total clusters: %i' % len(nclusters))
Q-index: q=0.33
Total clusters: 3
In [15]:
# Plots
plot_profiles(temp_df[EBQ_columns], clusters)
In [16]:
# save the output
temp_df['clusters_2yrs'] = clusters
temp_df[['subj','Sex','clusters_2yrs']].to_csv('../data/derived/clusters_2yrs.csv')

Do clutering separately for boys and girls

boys (Sex = 0)

In [17]:
temp_df_b = df[df['Sex'].isin([0])].copy()
print(temp_df_b.shape)
(281, 21)

Clustering and plotting for boys only

In [18]:
temp_df_b[EBQ_columns] = temp_df_b[EBQ_columns].copy().apply(zscore)
clusters = community_clustering(temp_df_b[EBQ_columns])

plot_profiles(temp_df_b[EBQ_columns], clusters)
Q-index: q=0.35
In [19]:
# save the output
temp_df_b['clusters_2yrs'] = clusters
temp_df_b[['subj','Sex','clusters_2yrs']].to_csv('../data/derived/clusters_2yrs_boys.csv')

girls (Sex = 1)

In [22]:
temp_df_g = df[df['Sex'].isin([1])].copy()
print(temp_df_g.shape)
(241, 21)

Clustering and plotting for gilrs only

In [23]:
temp_df_g[EBQ_columns] = temp_df_g[EBQ_columns].copy().apply(zscore)
clusters = community_clustering(temp_df_g[EBQ_columns])

plot_profiles(temp_df_g[EBQ_columns], clusters)
Q-index: q=0.30
In [24]:
# save the output
temp_df_g['clusters_2yrs'] = clusters
temp_df_g[['subj','Sex','clusters_2yrs']].to_csv('../data/derived/clusters_2yrs_girls.csv')

Read in 3 yrs ECBQ data

In [8]:
df = pd.read_csv('../data/raw/TempAndQuestions_all.csv')
EBQ_columns = ['ECBQ_' + column + '_3yr' for column in EBQ_scales]
# columns_to_use = np.hstack([['subj', 'Sex'], EBQ_columns])
columns_to_use = np.hstack([['subj', 'Sex'], EBQ_columns])
df = df[columns_to_use].copy()
print('Total included: %i' % len(df))
print(df.shape)
Total included: 780
(780, 20)

checking missing data

In [9]:
df['missing'] = df[EBQ_columns].isnull().sum(axis=1) > 2 # if missing more than 2 ECBQ variables then no interpolation
print('Number of cases with missing values: %i' % sum(df['missing']))

df = df[df['missing'] == False]
print('Total included: %i' % len(df))
print(df.shape)
Number of cases with missing values: 322
Total included: 458
(458, 21)

Interpolation the missing values

In [10]:
df[EBQ_columns] = impyute.mice(df[EBQ_columns].values, loop=5000)
#df = df.dropna()
print(df)
         subj  Sex  ECBQ_Impulsive_3yr  ECBQ_Activity_3yr  \
1     s002a5a    0                4.00           5.000000   
2     s003a5f    0                4.00           4.833333   
5     s007a5f    1                4.50           4.250000   
10    s012a7a    0                4.50           5.250000   
17    s019a7f    1                2.25           3.125000   
19    s021a7f    0                4.75           5.250000   
20    s022a7a    1                5.00           4.500000   
22    s024a7f    0                5.00           4.125000   
24    s026a7a    1                3.00           3.625000   
26    s028a7a    1                3.25           3.428571   
28    s030a7a    1                3.00           4.750000   
29    s031a7f    0                5.50           6.125000   
30    s032a7a    0                3.00           4.875000   
32    s034a5a    1                4.25           5.250000   
33    s035a7f    0                3.75           6.375000   
37    s039a7f    0                3.25           4.500000   
38   s040a12a    0                4.00           4.250000   
39    s041a5f    1                3.75           3.375000   
40   s042a12a    1                5.50           2.625000   
41   s043a12f    0                4.50           3.875000   
44   s046a12a    1                4.25           4.375000   
45   s047a12f    1                3.50           5.000000   
46   s048a12a    0                2.25           5.125000   
48    s050a5f    0                4.25           4.000000   
49   s052a12f    1                3.75           5.125000   
53    s056a5f    0                3.00           5.625000   
54   s057a12a    0                5.25           5.000000   
55   s058a12a    0                4.50           3.500000   
58   s061a12f    1                4.75           5.375000   
60    s063a5f    0                4.00           5.125000   
..        ...  ...                 ...                ...   
732  s837a12f    0                4.00           4.750000   
734   s839a7f    0                4.00           5.125000   
736   s841a7f    1                5.00           6.375000   
737   s842a7f    0                2.00           5.000000   
739   s844a7f    1                4.75           3.875000   
740   s845a7f    1                3.50           6.500000   
741   s846a7f    1                4.00           3.875000   
742  s847a12f    0                5.25           5.500000   
745  s850a12f    0                4.25           3.875000   
747  s852a12f    0                3.25           4.625000   
748  s853a12f    1                5.50           6.500000   
749  s854a12f    0                5.25           3.875000   
750  s855a12f    1                4.00           2.750000   
751   s856a7f    0                4.75           5.375000   
752  s857a12f    0                4.25           4.250000   
753   s858a7f    1                4.00           4.250000   
754   s859a7f    1                4.25           5.125000   
756  s861a12f    0                2.75           5.000000   
757  s862a12f    1                3.00           5.375000   
758  s863a12f    1                5.25           6.250000   
759  s864a12f    1                3.50           5.250000   
760  s865a12f    0                4.50           5.125000   
761  s866a12f    0                4.00           4.750000   
763  s868a12f    1                6.50           4.750000   
764  s869a12f    0                5.25           5.750000   
768  s873a12f    0                4.00           4.000000   
774  s879a12f    0                1.75           4.375000   
775  s880a12f    0                3.50           4.000000   
776  s881a12f    0                3.50           4.875000   
779  s884a12f    1                4.00           5.000000   

     ECBQ_High_Pleas_3yr  ECBQ_Sociability_3yr  ECBQ_Pos_Anticipate_3yr  \
1               4.000000              5.750000                 3.333333   
2               5.000000              6.000000                 6.800000   
5               5.333333              6.250000                 5.200000   
10              4.800000              5.500000                 6.200000   
17              3.333333              4.250000                 6.000000   
19              4.333333              4.750000                 6.000000   
20              4.000000              6.250000                 5.400000   
22              4.666667              6.000000                 5.800000   
24              3.666667              4.000000                 6.200000   
26              4.000000              5.666667                 5.600000   
28              5.166667              5.666667                 6.666667   
29              6.500000              6.250000                 5.600000   
30              5.166667              5.000000                 6.000000   
32              5.333333              6.250000                 6.600000   
33              6.166667              4.750000                 6.800000   
37              6.500000              7.000000                 6.800000   
38              4.666667              5.000000                 6.200000   
39              4.166667              6.750000                 5.600000   
40              3.833333              7.000000                 6.400000   
41              3.333333              7.000000                 6.400000   
44              5.666667              5.000000                 5.400000   
45              4.500000              6.000000                 5.800000   
46              6.000000              6.250000                 4.800000   
48              4.166667              6.500000                 5.800000   
49              5.666667              6.250000                 6.000000   
53              3.500000              5.500000                 6.000000   
54              5.333333              7.000000                 6.000000   
55              3.666667              6.500000                 5.200000   
58              3.833333              5.666667                 5.600000   
60              4.200000              5.000000                 6.600000   
..                   ...                   ...                      ...   
732             4.833333              4.000000                 4.666667   
734             4.500000              5.750000                 6.400000   
736             4.333333              6.750000                 7.000000   
737             5.500000              4.333333                 5.400000   
739             4.000000              6.250000                 5.800000   
740             5.800000              7.000000                 6.000000   
741             4.000000              6.250000                 6.400000   
742             5.166667              5.500000                 5.400000   
745             3.833333              6.000000                 5.000000   
747             4.166667              6.250000                 5.200000   
748             5.833333              6.750000                 6.800000   
749             5.333333              6.250000                 6.200000   
750             3.166667              6.000000                 5.200000   
751             5.000000              6.000000                 5.800000   
752             4.000000              5.500000                 5.600000   
753             5.333333              5.333333                 6.000000   
754             6.166667              7.000000                 6.600000   
756             5.333333              5.750000                 5.800000   
757             4.833333              6.500000                 5.800000   
758             5.166667              2.500000                 5.800000   
759             5.166667              4.500000                 5.400000   
760             5.833333              5.750000                 6.200000   
761             5.000000              4.250000                 5.800000   
763             4.500000              7.000000                 6.200000   
764             4.000000              4.333333                 6.600000   
768             3.833333              5.000000                 6.400000   
774             3.333333              6.750000                 6.400000   
775             5.000000              4.333333                 5.600000   
776             5.000000              5.500000                 6.600000   
779             6.166667              6.000000                 5.400000   

     ECBQ_Discomfort_3yr  ECBQ_Fear_3yr  ECBQ_Motor_3yr  ...  \
1               1.142857       1.125000        1.333333  ...   
2               1.142857       1.285714        1.166667  ...   
5               1.857143       1.375000        2.000000  ...   
10              3.000000       3.142857        2.600000  ...   
17              1.714286       2.571429        1.000000  ...   
19              2.142857       1.500000        1.333333  ...   
20              2.000000       1.375000        1.166667  ...   
22              1.714286       1.375000        1.333333  ...   
24              4.000000       2.625000        1.333333  ...   
26              3.000000       2.375000        1.833333  ...   
28              4.166667       1.571429        1.333333  ...   
29              2.166667       1.857143        1.833333  ...   
30              3.000000       2.000000        1.500000  ...   
32              3.285714       3.875000        1.833333  ...   
33              1.714286       2.125000        1.833333  ...   
37              2.571429       2.375000        1.500000  ...   
38              1.428571       1.625000        1.500000  ...   
39              2.000000       2.750000        1.833333  ...   
40              1.166667       1.875000        1.000000  ...   
41              1.714286       1.125000        1.500000  ...   
44              1.571429       2.500000        1.833333  ...   
45              2.428571       3.125000        2.333333  ...   
46              2.142857       2.500000        1.333333  ...   
48              2.142857       1.625000        2.833333  ...   
49              3.666667       3.750000        1.666667  ...   
53              1.285714       1.750000        1.833333  ...   
54              1.571429       1.250000        3.666667  ...   
55              1.142857       1.375000        1.166667  ...   
58              3.166667       2.000000        2.166667  ...   
60              2.571429       3.625000        2.000000  ...   
..                   ...            ...             ...  ...   
732             2.428571       1.750000        3.000000  ...   
734             2.857143       1.875000        2.500000  ...   
736             3.666667       2.714286        1.166667  ...   
737             2.142857       4.000000        2.333333  ...   
739             4.000000       3.750000        1.500000  ...   
740             3.000000       2.875000        2.166667  ...   
741             2.000000       2.500000        2.000000  ...   
742             1.857143       2.625000        1.666667  ...   
745             3.571429       3.500000        2.500000  ...   
747             3.142857       3.625000        2.666667  ...   
748             4.000000       3.375000        2.500000  ...   
749             2.571429       1.625000        1.500000  ...   
750             4.285714       3.500000        1.333333  ...   
751             2.000000       3.125000        2.833333  ...   
752             2.571429       2.125000        1.166667  ...   
753             1.285714       2.571429        3.166667  ...   
754             2.428571       1.750000        1.000000  ...   
756             1.857143       2.375000        1.666667  ...   
757             1.714286       3.000000        2.833333  ...   
758             3.666667       2.750000        2.750000  ...   
759             2.333333       3.125000        3.000000  ...   
760             2.571429       3.875000        2.166667  ...   
761             2.000000       3.125000        1.333333  ...   
763             2.714286       2.857143        1.833333  ...   
764             2.142857       3.200000        1.833333  ...   
768             1.571429       1.142857        1.000000  ...   
774             2.571429       2.375000        1.166667  ...   
775             3.142857       2.125000        1.166667  ...   
776             4.571429       2.000000        2.500000  ...   
779             2.333333       3.125000        1.166667  ...   

     ECBQ_Percept_Sens_3yr  ECBQ_Shyness_3yr  ECBQ_Soothability_3yr  \
1                     2.00               2.4               5.400000   
2                     3.60               2.4               5.400000   
5                     3.80               2.0               4.800000   
10                    4.80               2.0               4.200000   
17                    2.80               4.8               6.000000   
19                    5.60               2.0               5.200000   
20                    2.20               3.2               3.800000   
22                    3.60               3.8               4.200000   
24                    5.40               4.0               5.600000   
26                    4.00               3.6               4.800000   
28                    5.20               3.6               5.000000   
29                    4.00               2.4               4.400000   
30                    4.40               4.4               5.000000   
32                    6.00               3.2               4.000000   
33                    4.80               4.0               5.200000   
37                    4.00               2.4               5.200000   
38                    3.20               2.4               4.800000   
39                    4.40               1.8               6.200000   
40                    4.00               3.2               5.400000   
41                    4.80               2.2               5.400000   
44                    3.00               5.4               3.800000   
45                    4.00               3.4               4.800000   
46                    5.20               4.6               5.400000   
48                    4.40               3.6               5.200000   
49                    3.00               2.0               4.600000   
53                    4.80               4.8               5.200000   
54                    5.20               1.6               6.200000   
55                    1.80               2.2               5.800000   
58                    4.80               4.8               5.333333   
60                    5.20               3.6               4.200000   
..                     ...               ...                    ...   
732                   4.25               2.5               5.400000   
734                   5.00               3.6               5.000000   
736                   5.60               4.2               5.200000   
737                   3.20               5.6               4.400000   
739                   4.40               3.6               5.200000   
740                   1.60               1.6               5.200000   
741                   2.50               4.6               5.400000   
742                   3.20               2.4               5.400000   
745                   4.20               4.2               5.400000   
747                   3.80               2.6               5.600000   
748                   5.00               3.0               3.600000   
749                   2.20               1.8               6.200000   
750                   5.60               4.4               5.600000   
751                   4.60               2.6               5.000000   
752                   3.80               3.0               4.200000   
753                   3.40               5.0               5.200000   
754                   3.60               2.4               4.200000   
756                   4.00               5.4               5.200000   
757                   1.80               4.5               6.000000   
758                   6.20               4.8               5.200000   
759                   5.60               5.4               4.000000   
760                   4.00               3.2               5.000000   
761                   3.60               4.8               4.600000   
763                   2.80               4.5               5.000000   
764                   3.20               3.4               5.200000   
768                   2.20               4.4               5.000000   
774                   3.80               4.0               6.000000   
775                   3.00               5.6               6.000000   
776                   5.80               3.2               5.400000   
779                   4.80               3.4               5.800000   

     ECBQ_Frustration_3yr  ECBQ_Inhib_Cntrl_3yr  ECBQ_Attn_Shift_3yr  \
1                2.500000              5.666667             5.500000   
2                1.833333              4.000000             6.000000   
5                4.000000              4.333333             5.375000   
10               4.500000              4.333333             3.375000   
17               3.333333              6.000000             5.125000   
19               4.666667              3.333333             4.875000   
20               3.833333              4.166667             4.750000   
22               4.500000              5.600000             4.875000   
24               3.833333              5.666667             5.250000   
26               2.000000              4.833333             4.750000   
28               2.333333              5.500000             6.000000   
29               4.500000              3.166667             3.500000   
30               3.166667              4.833333             5.125000   
32               4.833333              4.000000             4.875000   
33               4.166667              4.666667             6.375000   
37               3.000000              4.666667             6.000000   
38               3.166667              4.833333             5.125000   
39               2.500000              4.666667             6.000000   
40               3.500000              5.333333             6.500000   
41               4.333333              3.666667             5.500000   
44               3.166667              4.000000             4.750000   
45               4.333333              4.833333             5.000000   
46               5.166667              3.000000             5.125000   
48               3.166667              5.666667             5.375000   
49               4.166667              5.000000             5.625000   
53               4.500000              3.500000             5.500000   
54               1.500000              3.833333             4.625000   
55               3.000000              4.166667             5.000000   
58               4.666667              4.333333             4.125000   
60               3.833333              3.500000             4.625000   
..                    ...                   ...                  ...   
732              5.333333              3.750000             4.750000   
734              4.000000              4.000000             5.250000   
736              3.000000              4.000000             5.625000   
737              3.500000              5.000000             5.125000   
739              3.166667              5.000000             5.500000   
740              2.833333              2.500000             4.875000   
741              2.333333              5.333333             4.500000   
742              2.833333              4.666667             5.625000   
745              3.000000              4.166667             5.000000   
747              3.500000              4.833333             5.125000   
748              6.500000              1.666667             5.000000   
749              2.666667              5.333333             5.875000   
750              3.000000              5.000000             5.375000   
751              3.666667              3.333333             4.125000   
752              3.666667              4.666667             5.142857   
753              3.200000              4.500000             4.571429   
754              5.166667              4.500000             6.375000   
756              3.666667              4.166667             4.625000   
757              2.333333              4.833333             4.142857   
758              3.500000              3.600000             5.714286   
759              3.833333              3.666667             4.625000   
760              3.000000              3.666667             5.500000   
761              3.500000              4.000000             4.625000   
763              4.666667              2.666667             4.375000   
764              3.500000              3.666667             5.125000   
768              3.500000              3.666667             5.428571   
774              2.500000              5.000000             5.625000   
775              2.833333              5.500000             5.000000   
776              3.333333              4.666667             4.625000   
779              5.000000              5.000000             5.375000   

     ECBQ_Low_Pleas_3yr  ECBQ_Cuddliness_3yr  ECBQ_Attn_Focus_3yr  missing  
1              3.000000             5.333333             5.666667    False  
2              5.250000             5.333333             5.833333    False  
5              5.000000             4.666667             5.666667    False  
10             2.000000             3.000000             4.500000    False  
17             6.000000             5.166667             5.000000    False  
19             2.333333             3.500000             5.333333    False  
20             4.666667             5.500000             5.833333    False  
22             4.833333             5.666667             5.500000    False  
24             4.833333             6.333333             6.333333    False  
26             4.333333             4.833333             5.833333    False  
28             6.000000             5.800000             4.833333    False  
29             3.000000             5.833333             2.833333    False  
30             4.166667             5.500000             4.333333    False  
32             4.000000             3.500000             4.500000    False  
33             3.333333             4.000000             3.666667    False  
37             5.666667             5.500000             5.333333    False  
38             4.166667             4.666667             4.333333    False  
39             5.500000             5.833333             5.666667    False  
40             3.500000             5.000000             6.833333    False  
41             4.833333             6.666667             5.833333    False  
44             3.250000             6.166667             4.500000    False  
45             5.166667             5.833333             5.666667    False  
46             5.500000             6.500000             5.166667    False  
48             4.500000             3.666667             3.166667    False  
49             3.200000             5.400000             5.166667    False  
53             3.500000             5.166667             5.333333    False  
54             5.666667             5.666667             6.166667    False  
55             4.166667             5.666667             4.666667    False  
58             4.500000             5.000000             3.666667    False  
60             4.500000             4.000000             4.500000    False  
..                  ...                  ...                  ...      ...  
732            4.333333             5.200000             4.333333    False  
734            4.000000             5.166667             5.666667    False  
736            4.166667             5.500000             6.500000    False  
737            4.500000             4.833333             5.166667    False  
739            5.500000             6.000000             5.166667    False  
740            2.500000             3.500000             4.000000    False  
741            5.600000             5.000000             4.166667    False  
742            4.833333             5.500000             5.166667    False  
745            3.800000             5.166667             5.666667    False  
747            3.666667             5.333333             4.333333    False  
748            5.666667             4.666667             4.000000    False  
749            5.000000             6.500000             6.000000    False  
750            4.666667             5.833333             5.666667    False  
751            5.000000             5.166667             4.500000    False  
752            4.750000             5.166667             5.000000    False  
753            5.500000             6.000000             5.666667    False  
754            5.200000             4.500000             5.833333    False  
756            4.833333             5.500000             4.500000    False  
757            3.666667             6.666667             3.500000    False  
758            5.500000             5.666667             4.833333    False  
759            5.166667             5.500000             4.000000    False  
760            5.166667             5.333333             5.666667    False  
761            4.166667             3.833333             5.166667    False  
763            3.500000             4.833333             4.833333    False  
764            3.500000             5.833333             5.666667    False  
768            4.833333             5.166667             5.666667    False  
774            5.166667             4.833333             5.500000    False  
775            6.000000             5.000000             6.333333    False  
776            5.400000             4.166667             5.000000    False  
779            5.833333             5.833333             4.833333    False  

[458 rows x 21 columns]
In [11]:
# Community clustering on the data
# temp_df = df[np.hstack(['subj', EBQ_columns, 'Age'])].copy()
temp_df = df.copy();
temp_df[EBQ_columns] = temp_df[EBQ_columns].copy().apply(zscore)
clusters = community_clustering(temp_df[EBQ_columns])
Q-index: q=0.32
In [12]:
# Plots
plot_profiles(temp_df[EBQ_columns], clusters)
In [13]:
# save the output
temp_df['clusters_3yrs'] = clusters
temp_df[['subj','Sex','clusters_3yrs']].to_csv('../data/derived/clusters_3yrs.csv')

Do clustering separately for 3yrs boys and girls

boys (Sex = 0)

In [48]:
temp_df_b = df[df['Sex'].isin([0])].copy()
print(temp_df_b.shape)
(246, 21)

Clustering and plotting for boys only

In [49]:
temp_df_b[EBQ_columns] = temp_df_b[EBQ_columns].copy().apply(zscore)
clusters = community_clustering(temp_df_b[EBQ_columns])

plot_profiles(temp_df_b[EBQ_columns], clusters)
Q-index: q=0.32
In [50]:
# save the output
temp_df_b['clusters_3yrs'] = clusters
temp_df_b[['subj','Sex','clusters_3yrs']].to_csv('../data/derived/clusters_3yrs_boys.csv')

girls (Sex = 1)

In [15]:
temp_df_g = df[df['Sex'].isin([1])].copy()
print(temp_df_g.shape)
(212, 21)

Clustering and plotting for gilrs only

In [16]:
temp_df_g[EBQ_columns] = temp_df_g[EBQ_columns].copy().apply(zscore)
clusters = community_clustering(temp_df_g[EBQ_columns])
Q-index: q=0.35
In [17]:
plot_profiles(temp_df_g[EBQ_columns], clusters)
In [18]:
# save the output
temp_df_g['clusters_3yrs'] = clusters
temp_df_g[['subj','Sex','clusters_3yrs']].to_csv('../data/derived/clusters_3yrs_girls.csv')